seu <- readRDS("/Users/victor/Documents/Master_2/Projet_long/Flux_metabo/data/seu_befor_flux.RDS")
seu
## An object of class Seurat 
## 29108 features across 30626 samples within 2 assays 
## Active assay: SCT (14554 features, 1874 variable features)
##  1 other assay present: RNA
##  2 dimensional reductions calculated: pca, umap
my_palette = c("D0"= "#E0E0E0",
               "D4"= "#ccFF99",
               "D7"= "#0066CC",
               "D10"= "#006633",
               "D14"= "#FF99FF",
               "D21"= "#994C00",
               "D32"= "#FF9999",
               "D90"= "#FF0000"
               )
DimPlot(seu, reduction = "umap", label = TRUE, group.by = "SCT_snn_res.0.8")

Flux

Visualisation seu et flux

predFlux <- read.csv("/Users/victor/Documents/Master_2/Projet_long/Flux_metabo/data/Results_scFEA/mouse_flux_gut.csv", header = T, row.names = 1)

# colnames(predFlux[,71:102])
predFlux[,71:102] <- NULL  # Elimination des modules 71 à 105 

# reste 136 modules

predFlux <- data.matrix(predFlux)
predFlux0 <- t(predFlux)
seu[["FLUX"]] <- CreateAssayObject(counts = predFlux0)
## Warning: Feature names cannot have underscores ('_'), replacing with dashes
## ('-')
seu
## An object of class Seurat 
## 29244 features across 30626 samples within 3 assays 
## Active assay: SCT (14554 features, 1874 variable features)
##  2 other assays present: RNA, FLUX
##  2 dimensional reductions calculated: pca, umap
DefaultAssay(seu) <- 'FLUX'
seu
## An object of class Seurat 
## 29244 features across 30626 samples within 3 assays 
## Active assay: FLUX (136 features, 0 variable features)
##  2 other assays present: RNA, SCT
##  2 dimensional reductions calculated: pca, umap
seu <- FindVariableFeatures(seu, selection.method = "vst", verbose = F)
## Warning in eval(predvars, data, env): NaNs produced
## Warning in hvf.info$variance.expected[not.const] <- 10^fit$fitted: number of
## items to replace is not a multiple of replacement length
seu  # regarder le nombre de variable
## An object of class Seurat 
## 29244 features across 30626 samples within 3 assays 
## Active assay: FLUX (136 features, 136 variable features)
##  2 other assays present: RNA, SCT
##  2 dimensional reductions calculated: pca, umap
seu$orig.ident <- factor(seu$orig.ident, levels = c("D0", "D4", "D7", "D10", "D14", "D21", "D32"))
seu <- ScaleData(seu, features = rownames(seu), assay = 'FLUX', verbose = F)
seu <- RunPCA(seu, features = VariableFeatures(object = seu), reduction.name = 'pca.flux', verbose = F)
## Warning: Cannot add objects with duplicate keys (offending key: PC_), setting
## key to 'pca.flux_'
ElbowPlot(seu, ndims = 30, reduction = 'pca.flux')

DimPlot(seu, reduction = "pca.flux", label = TRUE, group.by = "orig.ident", cols = my_palette)

Clustree and clusterization

seu <- RunUMAP(seu, dims = 1:7, assay = 'FLUX', reduction.name = "umap.flux", reduction = "pca.flux", verbose = F) # remplir les dims
## Warning: Cannot add objects with duplicate keys (offending key: UMAP_), setting
## key to 'umap.flux_'
seu <- FindNeighbors(seu, dims = 1:7, reduction = 'pca.flux', verbose = F, assay = 'FLUX')
seu <- FindClusters(seu , graph.name = 'FLUX_snn', verbose = F, resolution = 0.7)
head(Idents(seu))
## AAACCTGAGAGGTACC_D0 AAACCTGAGCGTAGTG_D0 AAACCTGAGCTAGTTC_D0 AAACCTGAGGATGGTC_D0 
##                  12                  21                  17                  21 
## AAACCTGAGGCAATTA_D0 AAACCTGAGGGTCGAT_D0 
##                  12                  16 
## Levels: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
DimPlot(seu, reduction = "umap.flux", label = TRUE)

DimPlot(seu, reduction = "umap.flux", label = TRUE, group.by = "orig.ident", cols = my_palette, label.box = T)

DimPlot(seu, reduction = "umap", label = TRUE, group.by = "FLUX_snn_res.0.5")

DimPlot(seu,reduction = "pca", split.by = "orig.ident", group.by = "orig.ident", cols = my_palette)

DimPlot(seu,reduction = "pca.flux", split.by = "orig.ident", group.by = "orig.ident", cols = my_palette)

DimPlot(seu,reduction = "umap", split.by = "orig.ident", group.by = "orig.ident", cols = my_palette)

DimPlot(seu,reduction = "umap.flux", split.by = "orig.ident", group.by = "orig.ident", cols = my_palette)

### Figure rapport :
DimPlot(seu, reduction = "umap", label.box = T, label = T, cols = my_palette, group.by = "orig.ident") +
  labs(title = "UMAP avec les jours de prélèvements")

DimPlot(seu, reduction = "umap", label.box = T, label = T, group.by = "FLUX_snn_res.0.5") +
  labs(title = "UMAP avec les clusters de flux métaboliques")

Analyse des flux

# Filtrage des modules avec un |CV| > e-10
data_c <- seu@assays$FLUX@counts
dt_c0 <- as.matrix(data_c)*1000  # x1000 car 10^-3 = seuil de significativité comme ça on passe à 1 en seuil
# calcul de |CV| et filtre sur valeur >1e-10 = DEVRAIT ÉLIMINER LES MODULES CONSTANTS SUR TOUTE CELLULE
yyy <- seu@meta.data$orig.ident 
levels(as.factor(seu@meta.data$orig.ident))
## [1] "D0"  "D4"  "D7"  "D10" "D14" "D21" "D32"
ppp_all <- c() 
for(ii in 1:nrow(dt_c0)){   # On  parcourt les lignes de dt_c0 (i.e. les modules)
    xxx <- dt_c0[ii,]       # Expression du module dans toutes les cellules
    final_df <- cbind(paste('X', 1:length(xxx), sep=''), xxx, yyy) # un data frame avec 1 col "numéro cellule"
    final_df <- as.data.frame(final_df)  # une colonne et l'expression du module
    final_df[,2] <- as.numeric(final_df[,2])  # une colone identifiant de la cellules 
    colnames(final_df) <- c('var', 'flux', 'cellType')  # nommées
    pp <- sd(final_df$flux)/abs(mean(final_df$flux))  # on calcule le |CV| de chaque module sur toutes les cellules
    ppp_all <- c(ppp_all, pp)  #  On remplit ppp_all avec cette valeur
}

a <- cbind(as.data.frame(ppp_all), rownames(seu))
b <- a < 5e-3
a <- a[b == T,]

tg_ids <- which(ppp_all > 5e-3) #  On filtre les [CV| > 5e-3

dim(predFlux)   # 136 M avant filtre sur CV
## [1] 30626   136
sel <- str_replace(rownames(dt_c0)[tg_ids],"M-" ,"M_")
predFlux <- predFlux[,sel]*1000    

dim(predFlux)  # 112 M après filtre CV
## [1] 30626   112
colnames(predFlux)  # liste tous les modules
##   [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_5"   "M_6"   "M_7"   "M_8"   "M_10" 
##  [10] "M_11"  "M_12"  "M_13"  "M_15"  "M_17"  "M_22"  "M_23"  "M_24"  "M_25" 
##  [19] "M_28"  "M_29"  "M_30"  "M_31"  "M_32"  "M_33"  "M_34"  "M_35"  "M_36" 
##  [28] "M_37"  "M_38"  "M_39"  "M_40"  "M_42"  "M_43"  "M_44"  "M_45"  "M_46" 
##  [37] "M_47"  "M_48"  "M_50"  "M_51"  "M_52"  "M_53"  "M_54"  "M_55"  "M_56" 
##  [46] "M_57"  "M_58"  "M_60"  "M_61"  "M_62"  "M_65"  "M_66"  "M_67"  "M_68" 
##  [55] "M_69"  "M_70"  "M_106" "M_107" "M_108" "M_109" "M_111" "M_112" "M_113"
##  [64] "M_114" "M_115" "M_116" "M_117" "M_118" "M_119" "M_120" "M_121" "M_122"
##  [73] "M_123" "M_125" "M_126" "M_129" "M_130" "M_131" "M_133" "M_134" "M_135"
##  [82] "M_136" "M_137" "M_138" "M_139" "M_140" "M_142" "M_143" "M_145" "M_146"
##  [91] "M_147" "M_148" "M_149" "M_150" "M_151" "M_152" "M_153" "M_155" "M_156"
## [100] "M_158" "M_159" "M_160" "M_161" "M_162" "M_164" "M_165" "M_166" "M_167"
## [109] "M_168" "M_169" "M_170" "M_171"
# (options()$max.print)
options(max.print = 1500)
summary(predFlux)  # regarder tous les modules
##       M_1              M_2               M_3              M_4        
##  Min.   : 25.04   Min.   :  4.295   Min.   : 24.71   Min.   : 10.28  
##  1st Qu.: 25.09   1st Qu.: 20.517   1st Qu.: 27.27   1st Qu.: 17.11  
##  Median : 25.28   Median : 44.389   Median : 30.02   Median : 23.06  
##  Mean   : 26.13   Mean   : 56.762   Mean   : 40.51   Mean   : 33.42  
##  3rd Qu.: 25.76   3rd Qu.: 81.345   3rd Qu.: 37.48   3rd Qu.: 36.65  
##  Max.   :133.75   Max.   :290.517   Max.   :186.53   Max.   :130.15  
##       M_5               M_6                 M_7              M_8       
##  Min.   :0.01394   Min.   :-0.013921   Min.   :0.5260   Min.   :1.131  
##  1st Qu.:0.01406   1st Qu.:-0.011552   1st Qu.:0.5260   1st Qu.:1.133  
##  Median :0.01422   Median :-0.010192   Median :0.5261   Median :1.135  
##  Mean   :0.01569   Mean   : 0.022563   Mean   :0.5280   Mean   :1.162  
##  3rd Qu.:0.01469   3rd Qu.:-0.006163   3rd Qu.:0.5264   3rd Qu.:1.143  
##  Max.   :0.29083   Max.   : 3.025696   Max.   :0.6587   Max.   :2.379  
##       M_10            M_11             M_12               M_13         
##  Min.   :1.127   Min.   :0.7856   Min.   :-0.01197   Min.   :0.005500  
##  1st Qu.:1.128   1st Qu.:0.7911   1st Qu.:-0.01195   1st Qu.:0.005588  
##  Median :1.129   Median :0.7966   Median :-0.01191   Median :0.005689  
##  Mean   :1.149   Mean   :0.8725   Mean   :-0.01053   Mean   :0.006812  
##  3rd Qu.:1.134   3rd Qu.:0.8196   3rd Qu.:-0.01175   3rd Qu.:0.005970  
##  Max.   :3.408   Max.   :3.9317   Max.   : 0.87619   Max.   :0.244886  
##       M_15                M_17                 M_22         
##  Min.   :0.0003334   Min.   :-0.0001676   Min.   :0.001309  
##  1st Qu.:0.0003334   1st Qu.:-0.0001676   1st Qu.:0.001309  
##  Median :0.0003343   Median :-0.0001676   Median :0.001309  
##  Mean   :0.0004137   Mean   :-0.0001675   Mean   :0.001311  
##  3rd Qu.:0.0003371   3rd Qu.:-0.0001676   3rd Qu.:0.001309  
##  Max.   :0.0085514   Max.   : 0.0006417   Max.   :0.005499  
##       M_23                M_24                 M_25             M_28       
##  Min.   :0.0004387   Min.   :-2.095e-05   Min.   : -5.01   Min.   :0.3455  
##  1st Qu.:0.0004387   1st Qu.: 5.821e-07   1st Qu.: 14.81   1st Qu.:0.3457  
##  Median :0.0004387   Median : 5.821e-07   Median : 15.03   Median :0.3460  
##  Mean   :0.0004390   Mean   : 1.183e-06   Mean   : 15.98   Mean   :0.3492  
##  3rd Qu.:0.0004387   3rd Qu.: 5.821e-07   3rd Qu.: 15.58   3rd Qu.:0.3469  
##  Max.   :0.0009239   Max.   : 1.278e-03   Max.   :286.76   Max.   :1.4276  
##       M_29                M_30                M_31                M_32         
##  Min.   :-0.005837   Min.   :-0.004897   Min.   :2.468e-05   Min.   :0.002669  
##  1st Qu.:-0.005661   1st Qu.:-0.004353   1st Qu.:4.005e-05   1st Qu.:0.002678  
##  Median :-0.005096   Median :-0.003656   Median :4.144e-05   Median :0.002690  
##  Mean   :-0.003656   Mean   :-0.002412   Mean   :4.362e-05   Mean   :0.002753  
##  3rd Qu.:-0.004068   3rd Qu.:-0.002492   3rd Qu.:4.470e-05   3rd Qu.:0.002725  
##  Max.   : 0.584066   Max.   : 0.522599   Max.   :2.747e-04   Max.   :0.010207  
##       M_33                 M_34                 M_35           
##  Min.   :-3.004e-02   Min.   :-0.0010002   Min.   :-0.0001984  
##  1st Qu.: 0.000e+00   1st Qu.:-0.0009816   1st Qu.: 0.0820532  
##  Median : 5.800e-08   Median :-0.0009406   Median : 0.0830889  
##  Mean   : 1.246e-05   Mean   :-0.0006163   Mean   : 0.0870153  
##  3rd Qu.: 4.660e-07   3rd Qu.:-0.0008717   3rd Qu.: 0.0850558  
##  Max.   : 8.212e-03   Max.   : 0.3674701   Max.   : 0.2907738  
##       M_36               M_37             M_38                M_39          
##  Min.   :-0.01916   Min.   : 8.431   Min.   :0.000e+00   Min.   :-0.004189  
##  1st Qu.:-0.01915   1st Qu.: 8.431   1st Qu.:0.000e+00   1st Qu.:-0.004083  
##  Median :-0.01912   Median : 8.431   Median :0.000e+00   Median :-0.003934  
##  Mean   :-0.01827   Mean   : 8.434   Mean   :3.122e-06   Mean   :-0.002884  
##  3rd Qu.:-0.01903   3rd Qu.: 8.431   3rd Qu.:0.000e+00   3rd Qu.:-0.003545  
##  Max.   : 0.57244   Max.   :19.330   Max.   :2.069e-03   Max.   : 0.623018  
##       M_40               M_42                 M_43             M_44         
##  Min.   :0.002589   Min.   :-0.0002244   Min.   : 5.516   Min.   :0.003643  
##  1st Qu.:0.002591   1st Qu.:-0.0002226   1st Qu.: 5.516   1st Qu.:0.003643  
##  Median :0.002597   Median :-0.0002189   Median : 5.517   Median :0.003643  
##  Mean   :0.002636   Mean   :-0.0001803   Mean   : 5.528   Mean   :0.003646  
##  3rd Qu.:0.002606   3rd Qu.:-0.0002095   3rd Qu.: 5.519   3rd Qu.:0.003643  
##  Max.   :0.005404   Max.   : 0.0196882   Max.   :11.158   Max.   :0.015758  
##       M_45               M_46            M_47                M_48        
##  Min.   :0.005366   Min.   :1.852   Min.   :-0.007752   Min.   :0.02081  
##  1st Qu.:0.005748   1st Qu.:1.861   1st Qu.:-0.007585   1st Qu.:0.02098  
##  Median :0.005756   Median :1.870   Median :-0.007214   Median :0.02135  
##  Mean   :0.005836   Mean   :1.942   Mean   :-0.005022   Mean   :0.02268  
##  3rd Qu.:0.005780   3rd Qu.:1.899   3rd Qu.:-0.005968   3rd Qu.:0.02236  
##  Max.   :0.010680   Max.   :5.896   Max.   : 3.163025   Max.   :0.23288  
##       M_50                M_51            M_52               M_53         
##  Min.   :-0.002099   Min.   :10.53   Min.   :-0.03565   Min.   :0.003379  
##  1st Qu.:-0.002099   1st Qu.:10.55   1st Qu.:-0.03565   1st Qu.:0.005310  
##  Median :-0.002099   Median :10.59   Median :-0.03565   Median :0.005316  
##  Mean   :-0.002096   Mean   :10.93   Mean   :-0.03563   Mean   :0.005302  
##  3rd Qu.:-0.002099   3rd Qu.:10.72   3rd Qu.:-0.03565   3rd Qu.:0.005318  
##  Max.   : 0.009149   Max.   :73.93   Max.   :-0.01571   Max.   :0.005327  
##       M_54                 M_55             M_56               M_57       
##  Min.   :-2.598e-03   Min.   : 7.583   Min.   :-0.09358   Min.   : 8.691  
##  1st Qu.: 0.000e+00   1st Qu.: 7.849   1st Qu.:-0.08116   1st Qu.: 8.726  
##  Median : 0.000e+00   Median : 8.158   Median :-0.06745   Median : 8.745  
##  Mean   : 7.034e-06   Mean   : 9.198   Mean   :-0.03180   Mean   : 8.920  
##  3rd Qu.: 2.910e-08   3rd Qu.: 8.985   3rd Qu.:-0.03802   3rd Qu.: 8.783  
##  Max.   : 2.310e-03   Max.   :66.579   Max.   :24.04296   Max.   :15.795  
##       M_58              M_60               M_61              M_62         
##  Min.   :-0.8048   Min.   :-0.03134   Min.   :-0.3521   Min.   :0.004234  
##  1st Qu.: 6.7866   1st Qu.:-0.02921   1st Qu.: 3.9894   1st Qu.:0.004236  
##  Median : 6.7866   Median :-0.02717   Median : 3.9947   Median :0.004243  
##  Mean   : 6.7853   Mean   :-0.01672   Mean   : 3.9485   Mean   :0.004316  
##  3rd Qu.: 6.7866   3rd Qu.:-0.02130   3rd Qu.: 3.9965   3rd Qu.:0.004267  
##  Max.   : 8.8803   Max.   :11.45640   Max.   : 3.9978   Max.   :0.013880  
##       M_65                 M_66               M_67                M_68      
##  Min.   :-0.0008708   Min.   :0.004172   Min.   :6.007e-05   Min.   :2.120  
##  1st Qu.:-0.0008615   1st Qu.:0.004172   1st Qu.:6.007e-05   1st Qu.:2.123  
##  Median :-0.0008289   Median :0.004172   Median :6.054e-05   Median :2.132  
##  Mean   :-0.0006557   Mean   :0.004182   Mean   :6.186e-05   Mean   :2.140  
##  3rd Qu.:-0.0007171   3rd Qu.:0.004174   3rd Qu.:6.054e-05   3rd Qu.:2.142  
##  Max.   : 0.1255050   Max.   :0.004945   Max.   :1.956e-04   Max.   :2.781  
##       M_69                M_70             M_106            M_107         
##  Min.   :-0.008762   Min.   :  6.609   Min.   : 2.575   Min.   :-229.696  
##  1st Qu.:-0.008361   1st Qu.:  7.602   1st Qu.: 6.216   1st Qu.:   4.065  
##  Median :-0.007706   Median :  9.491   Median : 6.244   Median :   5.771  
##  Mean   :-0.005717   Mean   : 13.208   Mean   : 6.375   Mean   :  11.910  
##  3rd Qu.:-0.006424   3rd Qu.: 14.116   3rd Qu.: 6.315   3rd Qu.:  10.999  
##  Max.   : 0.837252   Max.   :298.856   Max.   :20.986   Max.   : 200.087  
##      M_108              M_109           M_111            M_112      
##  Min.   :-0.03525   Min.   :4.881   Min.   : 7.662   Min.   :2.682  
##  1st Qu.:-0.03498   1st Qu.:4.882   1st Qu.: 7.664   1st Qu.:2.684  
##  Median :-0.03419   Median :4.883   Median : 7.671   Median :2.689  
##  Mean   :-0.02746   Mean   :4.895   Mean   : 7.739   Mean   :2.763  
##  3rd Qu.:-0.03193   3rd Qu.:4.887   3rd Qu.: 7.696   3rd Qu.:2.704  
##  Max.   :22.51494   Max.   :9.935   Max.   :11.060   Max.   :6.864  
##      M_113           M_114           M_115               M_116         
##  Min.   :11.53   Min.   :4.646   Min.   :1.164e-06   Min.   : -0.6724  
##  1st Qu.:11.59   1st Qu.:4.648   1st Qu.:4.884e-03   1st Qu.:  9.9500  
##  Median :11.71   Median :4.651   Median :4.921e-03   Median : 22.3136  
##  Mean   :12.82   Mean   :4.671   Mean   :4.787e-03   Mean   : 32.7286  
##  3rd Qu.:12.09   3rd Qu.:4.659   3rd Qu.:4.929e-03   3rd Qu.: 35.9266  
##  Max.   :47.28   Max.   :5.369   Max.   :4.936e-03   Max.   :686.0607  
##      M_117           M_118                M_119                M_120          
##  Min.   :4.540   Min.   :-0.0004480   Min.   :-0.0017118   Min.   :9.779e-06  
##  1st Qu.:4.543   1st Qu.:-0.0004433   1st Qu.:-0.0016540   1st Qu.:9.779e-06  
##  Median :4.548   Median :-0.0004368   Median :-0.0015926   Median :1.001e-05  
##  Mean   :4.569   Mean   :-0.0003598   Mean   :-0.0009789   Mean   :2.017e-05  
##  3rd Qu.:4.563   3rd Qu.:-0.0004135   3rd Qu.:-0.0014026   3rd Qu.:1.048e-05  
##  Max.   :5.769   Max.   : 0.1365766   Max.   : 0.5630106   Max.   :3.364e-03  
##      M_121            M_122               M_123               M_125       
##  Min.   : 3.517   Min.   :-0.012711   Min.   :-0.003418   Min.   :0.4619  
##  1st Qu.: 3.545   1st Qu.:-0.011563   1st Qu.:-0.003418   1st Qu.:0.4809  
##  Median : 3.634   Median :-0.010844   Median :-0.003416   Median :0.4872  
##  Mean   : 4.160   Mean   :-0.007586   Mean   :-0.003389   Mean   :0.5116  
##  3rd Qu.: 3.834   3rd Qu.:-0.009019   3rd Qu.:-0.003412   3rd Qu.:0.5011  
##  Max.   :34.628   Max.   : 3.256053   Max.   : 0.166498   Max.   :6.7842  
##      M_126             M_129              M_130               M_131      
##  Min.   :  5.713   Min.   :-0.05039   Min.   :0.0006305   Min.   :2.988  
##  1st Qu.:  5.915   1st Qu.:-0.04837   1st Qu.:0.0006352   1st Qu.:2.988  
##  Median :  7.193   Median :-0.04537   Median :0.0006510   Median :2.989  
##  Mean   :  9.073   Mean   :-0.03294   Mean   :0.0007262   Mean   :2.995  
##  3rd Qu.:  8.864   3rd Qu.:-0.03668   3rd Qu.:0.0006855   3rd Qu.:2.991  
##  Max.   :135.428   Max.   : 6.76611   Max.   :0.0245720   Max.   :5.565  
##      M_133            M_134               M_135                M_136        
##  Min.   : 9.992   Min.   :0.0004349   Min.   :-0.0003064   Min.   :0.00000  
##  1st Qu.: 9.993   1st Qu.:0.0004349   1st Qu.:-0.0003008   1st Qu.:0.01690  
##  Median : 9.994   Median :0.0004349   Median :-0.0002952   Median :0.01798  
##  Mean   :10.014   Mean   :0.0004391   Mean   :-0.0001740   Mean   :0.02171  
##  3rd Qu.: 9.996   3rd Qu.:0.0004359   3rd Qu.:-0.0002831   3rd Qu.:0.01970  
##  Max.   :11.144   Max.   :0.0006128   Max.   : 0.1027733   Max.   :0.27333  
##      M_137               M_138            M_139            M_140       
##  Min.   :0.000e+00   Min.   :0.1959   Min.   :0.8798   Min.   : 5.891  
##  1st Qu.:0.000e+00   1st Qu.:0.7771   1st Qu.:1.0558   1st Qu.: 5.962  
##  Median :0.000e+00   Median :0.7790   Median :1.0674   Median : 6.032  
##  Mean   :3.031e-09   Mean   :0.7628   Mean   :1.1109   Mean   : 6.535  
##  3rd Qu.:0.000e+00   3rd Qu.:0.7796   3rd Qu.:1.1018   3rd Qu.: 6.207  
##  Max.   :3.492e-06   Max.   :0.7806   Max.   :2.2606   Max.   :21.786  
##      M_142              M_143            M_145              M_146      
##  Min.   :-0.01922   Min.   : 6.873   Min.   :-0.04416   Min.   :2.231  
##  1st Qu.:-0.01921   1st Qu.: 6.954   1st Qu.:-0.04116   1st Qu.:2.231  
##  Median :-0.01917   Median : 7.037   Median :-0.03786   Median :2.231  
##  Mean   :-0.01543   Mean   : 7.772   Mean   :-0.02443   Mean   :2.231  
##  3rd Qu.:-0.01901   3rd Qu.: 7.235   3rd Qu.:-0.03181   3rd Qu.:2.231  
##  Max.   : 3.40538   Max.   :33.333   Max.   : 3.03803   Max.   :3.807  
##      M_147              M_148                M_149            M_150        
##  Min.   :-0.03378   Min.   :-0.0034906   Min.   : 10.74   Min.   :0.04920  
##  1st Qu.:-0.03378   1st Qu.:-0.0031590   1st Qu.: 11.65   1st Qu.:0.04920  
##  Median :-0.03378   Median :-0.0027958   Median : 12.70   Median :0.04921  
##  Mean   :-0.03373   Mean   :-0.0009872   Mean   : 16.59   Mean   :0.04964  
##  3rd Qu.:-0.03378   3rd Qu.:-0.0020787   3rd Qu.: 14.98   3rd Qu.:0.04922  
##  Max.   : 0.19999   Max.   : 0.6210581   Max.   :127.61   Max.   :0.08912  
##      M_151            M_152           M_153            M_155        
##  Min.   : 28.72   Min.   :5.916   Min.   : 25.89   Min.   :  56.53  
##  1st Qu.: 28.73   1st Qu.:8.699   1st Qu.: 28.03   1st Qu.:  61.88  
##  Median : 28.74   Median :8.710   Median : 32.15   Median :  66.21  
##  Mean   : 30.07   Mean   :8.732   Mean   : 43.62   Mean   : 108.51  
##  3rd Qu.: 28.78   3rd Qu.:8.732   3rd Qu.: 42.81   3rd Qu.:  77.33  
##  Max.   :174.46   Max.   :9.839   Max.   :312.93   Max.   :1492.98  
##      M_156             M_158               M_159           M_160      
##  Min.   :  41.76   Min.   :0.0005979   Min.   :14.98   Min.   :12.83  
##  1st Qu.:  72.72   1st Qu.:0.0052620   1st Qu.:15.04   1st Qu.:12.94  
##  Median : 101.75   Median :0.0052787   Median :15.08   Median :13.32  
##  Mean   : 170.33   Mean   :0.0051707   Mean   :15.37   Mean   :14.03  
##  3rd Qu.: 170.21   3rd Qu.:0.0052843   3rd Qu.:15.21   3rd Qu.:14.19  
##  Max.   :1158.88   Max.   :0.0052992   Max.   :25.60   Max.   :46.85  
##      M_161               M_162                M_164           
##  Min.   :-0.001695   Min.   :-3.260e-05   Min.   :-9.136e-04  
##  1st Qu.:-0.001675   1st Qu.:-5.821e-07   1st Qu.:-1.746e-07  
##  Median :-0.001647   Median :-4.657e-07   Median :-1.746e-07  
##  Mean   :-0.001350   Mean   :-6.030e-07   Mean   :-3.829e-07  
##  3rd Qu.:-0.001583   3rd Qu.:-4.657e-07   3rd Qu.:-1.746e-07  
##  Max.   : 0.202291   Max.   : 4.796e-05   Max.   :-1.746e-07  
##      M_165                M_166               M_167        
##  Min.   :-2.212e-06   Min.   :-0.006597   Min.   :0.03714  
##  1st Qu.:-2.212e-06   1st Qu.: 0.012845   1st Qu.:0.03716  
##  Median :-2.095e-06   Median : 0.012867   Median :0.03722  
##  Mean   : 8.993e-06   Mean   : 0.012682   Mean   :0.03828  
##  3rd Qu.:-1.979e-06   3rd Qu.: 0.012871   3rd Qu.:0.03754  
##  Max.   : 8.872e-03   Max.   : 0.012875   Max.   :0.09231  
##      M_168                M_169             M_170               M_171         
##  Min.   :-4.936e-05   Min.   :-0.5016   Min.   :-0.006795   Min.   :0.001969  
##  1st Qu.:-4.889e-05   1st Qu.: 0.8232   1st Qu.:-0.006735   1st Qu.:0.006586  
##  Median :-4.703e-05   Median : 0.8284   Median :-0.006611   Median :0.006625  
##  Mean   :-2.159e-05   Mean   : 0.8656   Mean   :-0.005518   Mean   :0.006906  
##  3rd Qu.:-3.912e-05   3rd Qu.: 0.8488   3rd Qu.:-0.006339   3rd Qu.:0.006730  
##  Max.   : 8.114e-03   Max.   : 2.0820   Max.   : 0.394367   Max.   :0.014395
moy <- c()
mini <- c()
maxi <- c()
for (i in 1:ncol(predFlux)) {
  moy <- c(moy, format(mean(predFlux[,i]), digits = 3))
  mini <- c(mini, format(min(predFlux[,i]), digits = 3))
  maxi <- c(maxi, format(max(predFlux[,i]), digits = 3))
}
resume <- as.data.frame(cbind(colnames(predFlux), moy, mini, maxi))
colnames(resume) <- c("module", "moyenne", "mini", "maxi")
resume[,2:4] <- lapply(resume[,2:4], as.numeric)

dim(resume) # 112 modules
## [1] 112   4
resume <- filter(resume, maxi >= 1)  # on récupère tous les modules significatif (sup à 1 car *1000)

dim(resume)  # nombre de module restant 55
## [1] 55  4
resume$module  # liste des modules restant
##  [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_6"   "M_8"   "M_10"  "M_11"  "M_25" 
## [10] "M_28"  "M_37"  "M_43"  "M_46"  "M_47"  "M_51"  "M_55"  "M_56"  "M_57" 
## [19] "M_58"  "M_60"  "M_61"  "M_68"  "M_70"  "M_106" "M_107" "M_108" "M_109"
## [28] "M_111" "M_112" "M_113" "M_114" "M_116" "M_117" "M_121" "M_122" "M_125"
## [37] "M_126" "M_129" "M_131" "M_133" "M_139" "M_140" "M_142" "M_143" "M_145"
## [46] "M_146" "M_149" "M_151" "M_152" "M_153" "M_155" "M_156" "M_159" "M_160"
## [55] "M_169"
sel[which(!(sel %in% resume$module))]  # liste des modules sup
##  [1] "M_5"   "M_7"   "M_12"  "M_13"  "M_15"  "M_17"  "M_22"  "M_23"  "M_24" 
## [10] "M_29"  "M_30"  "M_31"  "M_32"  "M_33"  "M_34"  "M_35"  "M_36"  "M_38" 
## [19] "M_39"  "M_40"  "M_42"  "M_44"  "M_45"  "M_48"  "M_50"  "M_52"  "M_53" 
## [28] "M_54"  "M_62"  "M_65"  "M_66"  "M_67"  "M_69"  "M_115" "M_118" "M_119"
## [37] "M_120" "M_123" "M_130" "M_134" "M_135" "M_136" "M_137" "M_138" "M_147"
## [46] "M_148" "M_150" "M_158" "M_161" "M_162" "M_164" "M_165" "M_166" "M_167"
## [55] "M_168" "M_170" "M_171"
predFlux <- predFlux[,resume$module]  # filtrage des modules
dim(predFlux)  # 55 M après filtre >1
## [1] 30626    55
flux <- as.data.frame(predFlux)
flux$cells <- rownames(flux)
flux <- relocate(flux, cells, .before = M_1)
mouse_module_info <- read.csv("/Users/victor/Documents/Master_2/Projet_long/Flux_metabo/data/mouse_modules_info.csv", header = T, row.names = 1, sep = ";")

supermodules <- mouse_module_info[colnames(flux)[-1],7]
supermodules <- str_c(rep("SM", 55), str_trim(supermodules, side = "left"))
for (i in 1:22) {
  print(paste0(paste0("SM", as.character(i)), " ", paste0("SM", as.character(i)) %in% supermodules))
}
## [1] "SM1 TRUE"
## [1] "SM2 TRUE"
## [1] "SM3 FALSE"
## [1] "SM4 FALSE"
## [1] "SM5 TRUE"
## [1] "SM6 TRUE"
## [1] "SM7 TRUE"
## [1] "SM8 TRUE"
## [1] "SM9 TRUE"
## [1] "SM10 TRUE"
## [1] "SM11 TRUE"
## [1] "SM12 FALSE"
## [1] "SM13 TRUE"
## [1] "SM14 TRUE"
## [1] "SM15 TRUE"
## [1] "SM16 TRUE"
## [1] "SM17 TRUE"
## [1] "SM18 TRUE"
## [1] "SM19 FALSE"
## [1] "SM20 TRUE"
## [1] "SM21 TRUE"
## [1] "SM22 TRUE"
# True = supermodule rpz False = supermodule non rpz
# graphing
long <- pivot_longer(flux, !cells, names_to = 'modules', values_to='fluxes')
long$SM <- rep(supermodules, nrow(flux))
# vérification
module_info <- as.data.frame(mouse_module_info)
module_info$SM_id <- as.numeric(module_info$SM_id)
for (i in 1:22) {
  print(paste0("SM",i," : ", as.character(length(which(supermodules == paste0("SM",i))))))
  print(length(which(long$SM == paste0("SM",i)))/42025 == length(which(supermodules == paste0("SM",i))))
}  # vérif attribution des modules au supermodules
## [1] "SM1 : 8"
## [1] FALSE
## [1] "SM2 : 2"
## [1] FALSE
## [1] "SM3 : 0"
## [1] TRUE
## [1] "SM4 : 0"
## [1] TRUE
## [1] "SM5 : 1"
## [1] FALSE
## [1] "SM6 : 1"
## [1] FALSE
## [1] "SM7 : 2"
## [1] FALSE
## [1] "SM8 : 1"
## [1] FALSE
## [1] "SM9 : 5"
## [1] FALSE
## [1] "SM10 : 2"
## [1] FALSE
## [1] "SM11 : 1"
## [1] FALSE
## [1] "SM12 : 0"
## [1] TRUE
## [1] "SM13 : 4"
## [1] FALSE
## [1] "SM14 : 1"
## [1] FALSE
## [1] "SM15 : 1"
## [1] FALSE
## [1] "SM16 : 6"
## [1] FALSE
## [1] "SM17 : 2"
## [1] FALSE
## [1] "SM18 : 2"
## [1] FALSE
## [1] "SM19 : 0"
## [1] TRUE
## [1] "SM20 : 8"
## [1] FALSE
## [1] "SM21 : 7"
## [1] FALSE
## [1] "SM22 : 1"
## [1] FALSE
theme_CA <- function (base_size = 11, base_family = "", base_line_size = base_size/22, 
                      base_rect_size = base_size/22) 
{
  
  theme_bw(base_size = base_size, base_family = base_family, 
           base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace% 
    theme(panel.border = element_rect(fill = NA, colour = "black", 
                                      linewidth = rel(1)), panel.grid.major = element_blank(),
          plot.title = element_text(size=10), 
          panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", linewidth = rel(1)), legend.key = element_blank(), 
          strip.background = element_rect(fill = "white", colour = "black", 
                                          linewidth = rel(2)), complete = TRUE)
}


gr <- list()
mod <- c(seq(1,2,1), seq(5,11,1), seq(13,18,1), seq(20,22,1))
for (i in mod) {
  data_int <- subset(long, SM == paste0("SM",i))
  gr[[i]] <- ggplot(data_int, aes(x=modules, y=fluxes)) + geom_boxplot() + theme_CA() + ggtitle(paste0("SM",i)) + ylab("Fluxes (*10^3)")
  # plot(gr[[i]])
  # png(paste0("/Users/Pin-Pin/2023/Results/23_01/Kurd6_531_metabo/y_flux_SM",i,".png"))
  # plot(gr[[i]])
  # dev.off()
}

print(gr)
## [[1]]

## 
## [[2]]

## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]

## 
## [[6]]

## 
## [[7]]

## 
## [[8]]

## 
## [[9]]

## 
## [[10]]

## 
## [[11]]

## 
## [[12]]
## NULL
## 
## [[13]]

## 
## [[14]]

## 
## [[15]]

## 
## [[16]]

## 
## [[17]]

## 
## [[18]]

## 
## [[19]]
## NULL
## 
## [[20]]

## 
## [[21]]

## 
## [[22]]

Histograme des flux

my_palette <- rev(my_palette)
tg_ids <- as.integer(str_remove(string = resume$module, pattern = "^M_"))
sel <- str_c("M-", as.character(tg_ids))
which(rownames(dt_c0) %in% sel)
##  [1]   1   2   3   4   6   8  10  11  25  28  37  43  46  47  51  55  56  57  58
## [20]  60  61  68  70  71  72  73  74  76  77  78  79  81  82  86  87  90  91  94
## [39]  96  98 104 105 107 108 110 111 114 116 117 118 120 121 124 125 134
dt <- dt_c0[which(rownames(dt_c0) %in% sel),]
dim(dt)
## [1]    55 30626
if(length(tg_ids) > 0){    # Vérification mais pas de "else"
   for (jj in 1:length(tg_ids)) {
    xxx <- dt[jj, ] 
    final_df <- data.frame(var = paste('X', 1:length(xxx), sep=''),  # le même que plus haut
                       flux = xxx,
                       day = yyy)
    order <- rev(c("D0", "D4", "D7", "D10", "D14", "D21", "D32"))     ######################## verif D
    final_df <- transform(final_df, day = factor(day, levels = order))
    title <- mouse_module_info[rownames(mouse_module_info)[tg_ids[jj]], 'M_name'] # récupère le noms du module
    aa <- ggplot(final_df, aes(x = flux, y = day, fill = day)) +
      geom_density_ridges() +
      theme_ridges() +
      theme(legend.position = 'none') +
      ggtitle(title) +
      theme(plot.title = element_text(hjust = 0.5)) +
      scale_fill_manual(values = my_palette) + 
      xlab("Flux (*10^3)") +
      geom_vline(xintercept = median(final_df$flux))
    i <- str_replace(rownames(dt)[jj], "-", "_")
    data_int <- subset(long, modules == i)
    titl <- as.character(length(which(data_int$fluxes < quantile(data_int$fluxes, 0.25) - 1.5 * IQR(data_int$fluxes) | data_int$fluxes > quantile(data_int$fluxes, 0.75) + 1.5 * IQR(data_int$fluxes))))
    bb <- ggplot(data_int, aes(x=modules, y=fluxes)) + geom_boxplot() + theme_CA() + ggtitle(paste0("# outliers = ", titl)) + ylab("Flux (*10^3)") + theme(axis.title.x = element_blank(), plot.title = element_text(size=5)) + stat_summary(fun = mean, geom = "point", shape = 18, size = 2.5, color = "#FC4E07")
    graph <- ggdraw() + 
  draw_plot(aa, 0, 0, 0.8, 1) +
  draw_plot(bb, 0.8, 0, 0.2, 0.9)
    plot(graph)
    # png(paste0("/Users/Pin-Pin/2023/Results/23_01/Kurd6_531_metabo/z_flux_", as.character(rownames(dt)[jj]), ".png"))
    # plot(graph)
    # dev.off()
   }
}
## Picking joint bandwidth of 0.146
## Picking joint bandwidth of 4.03

## Picking joint bandwidth of 1.45

## Picking joint bandwidth of 1.4

## Picking joint bandwidth of 0.00867

## Picking joint bandwidth of 0.00486

## Picking joint bandwidth of 0.00334

## Picking joint bandwidth of 0.0134

## Picking joint bandwidth of 0.479

## Picking joint bandwidth of 0.000494

## Picking joint bandwidth of 0.017

## Picking joint bandwidth of 0.00152

## Picking joint bandwidth of 0.0106

## Picking joint bandwidth of 0.000131

## Picking joint bandwidth of 0.0573

## Picking joint bandwidth of 0.149

## Picking joint bandwidth of 0.00237

## Picking joint bandwidth of 0.0354

## Picking joint bandwidth of 0.0119

## Picking joint bandwidth of 0.000587

## Picking joint bandwidth of 0.00986

## Picking joint bandwidth of 0.00188

## Picking joint bandwidth of 0.598

## Picking joint bandwidth of 0.0235

## Picking joint bandwidth of 1.1

## Picking joint bandwidth of 0.000521

## Picking joint bandwidth of 0.00159

## Picking joint bandwidth of 0.0141

## Picking joint bandwidth of 0.0139

## Picking joint bandwidth of 0.206

## Picking joint bandwidth of 0.00367

## Picking joint bandwidth of 1.48

## Picking joint bandwidth of 0.00328

## Picking joint bandwidth of 0.09

## Picking joint bandwidth of 0.000331

## Picking joint bandwidth of 0.0029

## Picking joint bandwidth of 0.26

## Picking joint bandwidth of 0.00113

## Picking joint bandwidth of 0.000694

## Picking joint bandwidth of 0.00482

## Picking joint bandwidth of 0.00962

## Picking joint bandwidth of 0.0761

## Picking joint bandwidth of 0.000243

## Picking joint bandwidth of 0.136

## Picking joint bandwidth of 0.00157

## Picking joint bandwidth of 0.00113

## Picking joint bandwidth of 0.631

## Picking joint bandwidth of 0.346

## Picking joint bandwidth of 0.00562

## Picking joint bandwidth of 1.75

## Picking joint bandwidth of 8.09

## Picking joint bandwidth of 10.1

## Picking joint bandwidth of 0.0363

## Picking joint bandwidth of 0.129

## Picking joint bandwidth of 0.00493

heatmap

mat <- data.matrix(predFlux)
coul <- c("#E0E0E0", "#CCFF99", "#0066CC", "#006633", "#FF99FF", "#994C00","#FF9999")  # 7 col car 7D
coul11 <- c(coul, "#FAE1C7", "#000000", "#FF8000", "#FAA4A4", "#d18975", "#2d543d", "#ff0055", "#5DA5DA", "#B276B2", "#F17CB0", "#009E73", "#F0E442", "#D55E00")  #11 couleurs en ajouter en fct du nm de SM
cell_group <- as.numeric(factor(as.factor(yyy), levels = order))
colSide <- coul[cell_group]
module_group <- as.numeric(factor(as.factor(supermodules), c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21", "SM22")))   #SM a modif en fct des resultats
colMod <- coul11[module_group]

## Gradient colors
brek <- c(min(mat), 0, 1, 1.5, 2, 2.5, 3, 4, 5, 10, 100, max(mat))
hm.colors = c("#00CCCC", "#FFFFFF", "#FFCCCC", "#FF9999", "#FF6666", "#FF3333", "#FF0000", "#CC0000", "#990000", "#660000", "#000000")

## Heatmap
heatmap.2(mat, trace = 'none', breaks = brek, col = hm.colors, RowSideColors=colSide, ColSideColors=colMod, margins=c(0.5,0.5), key = TRUE, symkey = FALSE, density.info = 'none')
legend(0,0.5, legend=order, fill=coul, cex=0.7)
legend(0.37,1, legend=c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21", "SM22"), fill=coul11, cex = 0.6, ncol =7)

Suppression des modules blanc sur heatmap

# View(mat)
# summary(mat)
mat_2<-mat
# On cherche à id les 9 modules bleu et les 4 modules blanc pour les supprimer
# mean < 0 : 47 / 56 / 60 / 108 / 122 / 129 / 142 / 145   (8)
# mean < 1 : 6 / 11 / 28 / 125 / 169                      (5)

colnames(mat)
##  [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_6"   "M_8"   "M_10"  "M_11"  "M_25" 
## [10] "M_28"  "M_37"  "M_43"  "M_46"  "M_47"  "M_51"  "M_55"  "M_56"  "M_57" 
## [19] "M_58"  "M_60"  "M_61"  "M_68"  "M_70"  "M_106" "M_107" "M_108" "M_109"
## [28] "M_111" "M_112" "M_113" "M_114" "M_116" "M_117" "M_121" "M_122" "M_125"
## [37] "M_126" "M_129" "M_131" "M_133" "M_139" "M_140" "M_142" "M_143" "M_145"
## [46] "M_146" "M_149" "M_151" "M_152" "M_153" "M_155" "M_156" "M_159" "M_160"
## [55] "M_169"
a <- mat_2[,c(5,8,10,36,55)]
colnames(a)
## [1] "M_6"   "M_11"  "M_28"  "M_125" "M_169"
mat_2 <- mat_2[,-c(5,8,10,36,55)]
colnames(mat_2)
##  [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_8"   "M_10"  "M_25"  "M_37"  "M_43" 
## [10] "M_46"  "M_47"  "M_51"  "M_55"  "M_56"  "M_57"  "M_58"  "M_60"  "M_61" 
## [19] "M_68"  "M_70"  "M_106" "M_107" "M_108" "M_109" "M_111" "M_112" "M_113"
## [28] "M_114" "M_116" "M_117" "M_121" "M_122" "M_126" "M_129" "M_131" "M_133"
## [37] "M_139" "M_140" "M_142" "M_143" "M_145" "M_146" "M_149" "M_151" "M_152"
## [46] "M_153" "M_155" "M_156" "M_159" "M_160"
supermodules <- mouse_module_info[colnames(mat_2),7]
supermodules <- str_c(rep("SM", 50), str_trim(supermodules, side = "left"))

coul <- c("#E0E0E0", "#CCFF99", "#0066CC", "#006633", "#FF99FF", "#994C00","#FF9999")  # 7 col car 7D
coul11 <- c(coul, "#FAE1C7", "#000000", "#FF8000", "#FAA4A4", "#d18975", "#2d543d", "#ff0055", "#5DA5DA", "#B276B2", "#F17CB0", "#D55E00")  #17 couleurs en ajouter en fct du nm de SM  ## 20 COLORS  # , "#009E73", "#F0E442"
cell_group <- as.numeric(factor(as.factor(yyy), levels = order))
colSide <- coul[cell_group]
module_group <- as.numeric(factor(as.factor(supermodules), c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21")))   #SM a modif en fct des resultats
colMod <- coul11[module_group]

## Gradient colors
brek <- c(min(mat), 0, 1, 1.5, 2, 2.5, 3, 4, 5, 10, 100, max(mat))
hm.colors = c("#00CCCC", "#FFFFFF", "#FFCCCC", "#FF9999", "#FF6666", "#FF3333", "#FF0000", "#CC0000", "#990000", "#660000", "#000000")

## Heatmap
heatmap.2(mat_2, trace = 'none', breaks = brek, col = hm.colors, RowSideColors=colSide, ColSideColors=colMod, margins=c(0.5,0.5), key = TRUE, symkey = FALSE, density.info = 'none')
legend(0,0.5, legend=order, fill=coul, cex=0.7)
legend(0.37,1, legend= c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21"), fill=coul11, cex = 0.6, ncol =7)

Suppression des modules BLEU sur heatmap

# On cherche à id les 9 modules bleu et les 4 modules blanc pour les supprimer
# mean < 0 : 47 / 56 / 60 / 108 / 122 / 129 / 142 / 145   (8)
# mean < 1 : 6 / 11 / 28 / 125 / 169                      (5)

colnames(mat_2)
##  [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_8"   "M_10"  "M_25"  "M_37"  "M_43" 
## [10] "M_46"  "M_47"  "M_51"  "M_55"  "M_56"  "M_57"  "M_58"  "M_60"  "M_61" 
## [19] "M_68"  "M_70"  "M_106" "M_107" "M_108" "M_109" "M_111" "M_112" "M_113"
## [28] "M_114" "M_116" "M_117" "M_121" "M_122" "M_126" "M_129" "M_131" "M_133"
## [37] "M_139" "M_140" "M_142" "M_143" "M_145" "M_146" "M_149" "M_151" "M_152"
## [46] "M_153" "M_155" "M_156" "M_159" "M_160"
a <- mat_2[,c(11, 14, 17, 23, 32, 34, 39, 41)]
colnames(a)
## [1] "M_47"  "M_56"  "M_60"  "M_108" "M_122" "M_129" "M_142" "M_145"
mat_2 <- mat_2[,-c(11, 14, 17, 23, 32, 34, 39, 41)]
colnames(mat_2)
##  [1] "M_1"   "M_2"   "M_3"   "M_4"   "M_8"   "M_10"  "M_25"  "M_37"  "M_43" 
## [10] "M_46"  "M_51"  "M_55"  "M_57"  "M_58"  "M_61"  "M_68"  "M_70"  "M_106"
## [19] "M_107" "M_109" "M_111" "M_112" "M_113" "M_114" "M_116" "M_117" "M_121"
## [28] "M_126" "M_131" "M_133" "M_139" "M_140" "M_143" "M_146" "M_149" "M_151"
## [37] "M_152" "M_153" "M_155" "M_156" "M_159" "M_160"
supermodules <- mouse_module_info[colnames(mat_2),7]
supermodules <- str_c(rep("SM", 42), str_trim(supermodules, side = "left"))

coul <- c("#E0E0E0", "#CCFF99", "#0066CC", "#006633", "#FF99FF", "#994C00","#FF9999")  # 7 col car 7D
coul11 <- c(coul, "#FAE1C7", "#000000", "#FF8000", "#FAA4A4", "#d18975", "#2d543d", "#ff0055", "#5DA5DA", "#B276B2", "#F17CB0", "#D55E00")  #17 couleurs en ajouter en fct du nm de SM  ## 20 COLORS  # , "#009E73", "#F0E442"
cell_group <- as.numeric(factor(as.factor(yyy), levels = order))
colSide <- coul[cell_group]
module_group <- as.numeric(factor(as.factor(supermodules), c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21")))   #SM a modif en fct des resultats
colMod <- coul11[module_group]

## Gradient colors
brek <- c(min(mat), 0, 1, 1.5, 2, 2.5, 3, 4, 5, 10, 100, max(mat))
hm.colors = c("#00CCCC", "#FFFFFF", "#FFCCCC", "#FF9999", "#FF6666", "#FF3333", "#FF0000", "#CC0000", "#990000", "#660000", "#000000")

## Heatmap
heatmap.2(mat_2, trace = 'none', breaks = brek, col = hm.colors, RowSideColors=colSide, ColSideColors=colMod, margins=c(0.5,0.5), key = TRUE, symkey = FALSE, density.info = 'none')
legend(0,0.5, legend=order, fill=coul, cex=0.7)
legend(0.37,1, legend= c("SM1", "SM2", "SM5", "SM6", "SM7", "SM8", "SM9", "SM10", "SM11", "SM13", "SM14", "SM15", "SM16", "SM17", "SM18", "SM20", "SM21"), fill=coul11, cex = 0.6, ncol =7)

Histograme des flux finaux

mat_2 <- mat_2[,-c(8, 14, 34)]  ## Elimination des modules 37/58/146

my_palette <- rev(my_palette)
name_mod_2 <- colnames(mat_2)
tg_ids_2 <- as.integer(str_remove(string = name_mod_2, pattern = "^M_"))
sel_2 <- str_c("M-", as.character(tg_ids_2))
which(rownames(dt_c0) %in% sel)
##  [1]   1   2   3   4   6   8  10  11  25  28  37  43  46  47  51  55  56  57  58
## [20]  60  61  68  70  71  72  73  74  76  77  78  79  81  82  86  87  90  91  94
## [39]  96  98 104 105 107 108 110 111 114 116 117 118 120 121 124 125 134
dt_2 <- dt_c0[which(rownames(dt_c0) %in% sel_2),]
dim(dt_2)
## [1]    39 30626
if(length(tg_ids_2) > 0){    # Vérification mais pas de "else"
   for (jj in 1:length(tg_ids_2)) {
    xxx <- dt_2[jj, ] 
    final_df <- data.frame(var = paste('X', 1:length(xxx), sep=''),  # le même que plus haut
                       flux = xxx,
                       day = yyy)
    order <- rev(c("D0", "D4", "D7", "D10", "D14", "D21", "D32"))     ######################## verif D
    final_df <- transform(final_df, day = factor(day, levels = order))
    title <- mouse_module_info[rownames(mouse_module_info)[tg_ids_2[jj]], 'M_name'] # récupère le noms du module
    aa <- ggplot(final_df, aes(x = flux, y = day, fill = day)) +
      geom_density_ridges() +
      theme_ridges() +
      theme(legend.position = 'none') +
      ggtitle(title) +
      theme(plot.title = element_text(hjust = 0.5)) +
      scale_fill_manual(values = my_palette) + 
      xlab("Flux (*10^3)") +
      geom_vline(xintercept = median(final_df$flux))
    i <- str_replace(rownames(dt_2)[jj], "-", "_")
    data_int <- subset(long, modules == i)
    titl <- as.character(length(which(data_int$fluxes < quantile(data_int$fluxes, 0.25) - 1.5 * IQR(data_int$fluxes) | data_int$fluxes > quantile(data_int$fluxes, 0.75) + 1.5 * IQR(data_int$fluxes))))
    bb <- ggplot(data_int, aes(x=modules, y=fluxes)) + geom_boxplot() + theme_CA() + ggtitle(paste0("# outliers = ", titl)) + ylab("Flux (*10^3)") + theme(axis.title.x = element_blank(), plot.title = element_text(size=5)) + stat_summary(fun = mean, geom = "point", shape = 18, size = 2.5, color = "#FC4E07")
    graph <- ggdraw() + 
  draw_plot(aa, 0, 0, 0.8, 1) +
  draw_plot(bb, 0.8, 0, 0.2, 0.9)
    plot(graph)
    # png(paste0("/Users/Pin-Pin/2023/Results/23_01/Kurd6_531_metabo/z_flux_", as.character(rownames(dt)[jj]), ".png"))
    # plot(graph)
    # dev.off()
   }
}
## Picking joint bandwidth of 0.146
## Picking joint bandwidth of 4.03

## Picking joint bandwidth of 1.45

## Picking joint bandwidth of 1.4

## Picking joint bandwidth of 0.00486

## Picking joint bandwidth of 0.00334

## Picking joint bandwidth of 0.479

## Picking joint bandwidth of 0.00152

## Picking joint bandwidth of 0.0106

## Picking joint bandwidth of 0.0573

## Picking joint bandwidth of 0.149

## Picking joint bandwidth of 0.0354

## Picking joint bandwidth of 0.00986

## Picking joint bandwidth of 0.00188

## Picking joint bandwidth of 0.598

## Picking joint bandwidth of 0.0235

## Picking joint bandwidth of 1.1

## Picking joint bandwidth of 0.00159

## Picking joint bandwidth of 0.0141

## Picking joint bandwidth of 0.0139

## Picking joint bandwidth of 0.206

## Picking joint bandwidth of 0.00367

## Picking joint bandwidth of 1.48

## Picking joint bandwidth of 0.00328

## Picking joint bandwidth of 0.09

## Picking joint bandwidth of 0.26

## Picking joint bandwidth of 0.000694

## Picking joint bandwidth of 0.00482

## Picking joint bandwidth of 0.00962

## Picking joint bandwidth of 0.0761

## Picking joint bandwidth of 0.136

## Picking joint bandwidth of 0.631

## Picking joint bandwidth of 0.346

## Picking joint bandwidth of 0.00562

## Picking joint bandwidth of 1.75

## Picking joint bandwidth of 8.09

## Picking joint bandwidth of 10.1

## Picking joint bandwidth of 0.0363

## Picking joint bandwidth of 0.129

Barplot

coul11 <- c(coul, "#FAE1C7", "#000000", "#FF8000", "#FAA4A4", "#d18975", "#2d543d", "#ff0055", "#5DA5DA", "#B276B2", "#F17CB0", "#D55E00", "#009E73", "#F0E442")  #17 couleurs en ajouter en fct du nm de SM  ## 20 COLORS  # 

 cell <- cbind(Cells(seu), seu@meta.data$orig.ident)
colnames(cell) <- c("barcode", "dataset")
day <- seu@meta.data$orig.ident
cell <- as.tibble(cell)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
cell$day <- day
cell$rna_cluster <- seu@meta.data$SCT_snn_res.0.8
cell$flux_cluster <- seu@meta.data$FLUX_snn_res.0.5

unique(cell$flux_cluster)
##  [1] 12 16 4  11 5  14 10 17 6  3  8  13 7  2  1  15 9  0 
## Levels: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
unique(cell$rna_cluster)
##  [1] 6  0  14 11 4  9  15 1  8  19 17 18 7  16 3  12 2  5  10 13
## Levels: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ggplot(cell, aes(x = day, fill = rna_cluster)) +
  geom_bar() +
  labs(title = "Repartition des cluster RNA en fonction des jours", 
       y = "RNA clusters") +
  theme_CA() +
  scale_fill_manual(values = coul11) +
  theme(axis.title.x = element_blank())

ggplot(cell, aes(x = day, fill = flux_cluster)) +
  geom_bar() +
  labs(title = "Repartition des cluster de flux en fonction des jours", 
       y = "Flux clusters") +
  theme_CA() +  
  scale_fill_manual(values = coul11) +
  theme(axis.title.x = element_blank())

saveRDS(seu, "/Users/victor/Documents/Master_2/Projet_long/Flux_metabo/results/seu__flux.RDS")
sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] clustree_0.5.0     ggraph_2.1.0       gplots_3.1.3       ggridges_0.5.4    
##  [5] reshape_0.8.9      ggpubr_0.6.0       rstatix_0.7.2      cowplot_1.1.1     
##  [9] gridExtra_2.3      RColorBrewer_1.1-3 SeuratObject_4.1.3 Seurat_4.3.0      
## [13] Matrix_1.5-1       lubridate_1.9.2    forcats_1.0.0      stringr_1.5.0     
## [17] dplyr_1.1.1        purrr_1.0.1        readr_2.1.4        tidyr_1.3.0       
## [21] tibble_3.2.1       ggplot2_3.4.2      tidyverse_2.0.0    tinytex_0.44      
## [25] rmarkdown_2.21    
## 
## loaded via a namespace (and not attached):
##   [1] backports_1.4.1        plyr_1.8.8             igraph_1.4.1          
##   [4] lazyeval_0.2.2         sp_1.6-0               splines_4.1.2         
##   [7] listenv_0.9.0          scattermore_0.8        digest_0.6.31         
##  [10] htmltools_0.5.5        viridis_0.6.2          fansi_1.0.4           
##  [13] checkmate_2.1.0        magrittr_2.0.3         tensor_1.5            
##  [16] cluster_2.1.4          ROCR_1.0-11            tzdb_0.3.0            
##  [19] graphlayouts_0.8.4     globals_0.16.2         matrixStats_0.63.0    
##  [22] timechange_0.2.0       spatstat.sparse_3.0-1  colorspace_2.1-0      
##  [25] ggrepel_0.9.3          xfun_0.38              crayon_1.5.2          
##  [28] jsonlite_1.8.4         progressr_0.13.0       spatstat.data_3.0-1   
##  [31] survival_3.5-5         zoo_1.8-11             glue_1.6.2            
##  [34] polyclip_1.10-4        gtable_0.3.3           leiden_0.4.3          
##  [37] car_3.1-2              future.apply_1.10.0    abind_1.4-5           
##  [40] scales_1.2.1           DBI_1.1.3              spatstat.random_3.1-4 
##  [43] miniUI_0.1.1.1         Rcpp_1.0.10            viridisLite_0.4.1     
##  [46] xtable_1.8-4           reticulate_1.28        htmlwidgets_1.6.2     
##  [49] httr_1.4.5             ellipsis_0.3.2         ica_1.0-3             
##  [52] farver_2.1.1           pkgconfig_2.0.3        sass_0.4.5            
##  [55] uwot_0.1.14            deldir_1.0-6           utf8_1.2.3            
##  [58] labeling_0.4.2         tidyselect_1.2.0       rlang_1.1.0           
##  [61] reshape2_1.4.4         later_1.3.0            munsell_0.5.0         
##  [64] tools_4.1.2            cachem_1.0.7           cli_3.6.1             
##  [67] generics_0.1.3         broom_1.0.4            evaluate_0.20         
##  [70] fastmap_1.1.1          yaml_2.3.7             goftest_1.2-3         
##  [73] knitr_1.42             fitdistrplus_1.1-8     tidygraph_1.2.3       
##  [76] caTools_1.18.2         RANN_2.6.1             pbapply_1.7-0         
##  [79] future_1.32.0          nlme_3.1-162           mime_0.12             
##  [82] compiler_4.1.2         rstudioapi_0.14        plotly_4.10.1         
##  [85] png_0.1-8              ggsignif_0.6.4         spatstat.utils_3.0-2  
##  [88] tweenr_2.0.2           bslib_0.4.2            stringi_1.7.12        
##  [91] highr_0.10             lattice_0.20-45        vctrs_0.6.1           
##  [94] pillar_1.9.0           lifecycle_1.0.3        spatstat.geom_3.1-0   
##  [97] lmtest_0.9-40          jquerylib_0.1.4        RcppAnnoy_0.0.20      
## [100] data.table_1.14.8      bitops_1.0-7           irlba_2.3.5.1         
## [103] httpuv_1.6.9           patchwork_1.1.2        R6_2.5.1              
## [106] promises_1.2.0.1       KernSmooth_2.23-20     parallelly_1.35.0     
## [109] codetools_0.2-19       MASS_7.3-58.3          gtools_3.9.4          
## [112] withr_2.5.0            sctransform_0.3.5      parallel_4.1.2        
## [115] hms_1.1.3              grid_4.1.2             carData_3.0-5         
## [118] Rtsne_0.16             spatstat.explore_3.1-0 ggforce_0.4.1         
## [121] shiny_1.7.4